﻿var appIdFacebook = '143142795773655'        

window.fbAsyncInit = function() {
    FB.init({appId: appIdFacebook, status: true, cookie: true, xfbml: true});

    /* All the events registered */
    FB.Event.subscribe('auth.login', function(response) {
        // do something with response
        loginfb();
    });
    FB.Event.subscribe('auth.logout', function(response) {
        // do something with response
        logout();
    });

    FB.getLoginStatus(function(response) {
        if (response.session) {
            // logged in and connected user, someone you know
            loginfb();
        }
    });
};
(function() {
    var e = document.createElement('script');
    e.type = 'text/javascript';
    e.src = document.location.protocol +
        '//connect.facebook.net/es_ES/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());

function loginfb(){
    FB.api('/me', function (response) {
        var pagina = new String(window.location);
        if (pagina.indexOf('Inicio') != -1) {
            var parametros = "{" +
                                "'Nombre':'" + response.first_name + "'," +
                                "'Apellido':'" + response.last_name + "'," +
                                "'Mail':'" + response.email + "'," +
                                "'IdFacebook':'" + response.id + "'" +
                             "}";

            LlamarWebService("../Servicios/Usuario.asmx/LogInFacebook", parametros, GoToHomeFromLogIn, FuncionFallo);
        }
    });
}
function logout(){
    document.getElementById('login').style.display = "none";
}

function Feed(){
    FB.ui(
      {
       method: 'feed',
       message: 'getting educated about Facebook Connect',
       name: 'Connect',
       caption: 'The Facebook Connect JavaScript SDK',
          description: (
          'A small JavaScript library that allows you to harness ' +
          'the power of Facebook, bringing the user\'s identity, ' +
          'social graph and distribution power to your site.'
       ),
       link: 'http://www.fbrell.com/',
       picture: 'http://www.fbrell.com/f8.jpg',
       actions: [
            { name: 'fbrell', link: 'http://www.fbrell.com/' }
       ],
      user_message_prompt: 'Share your thoughts about RELL'
      },
      function(response) {

      }
    );

}

//stream publish method
function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){
    FB.ui(
    {
        method: 'stream.publish',
        message: '',
        attachment: {
            name: name,
            caption: '',
            description: (description),
            href: hrefLink
        },
        action_links: [
            { text: hrefTitle, href: hrefLink }
        ],
        user_prompt_message: userPrompt
    },
    function(response) {

    });

}
function showStream(){
    FB.api('/me', function(response) {
        streamPublish(response.name, 'Red social de artistas', 'ComunArte', 'http://piazza.com/', "");
    });
}

function shareFacebook(){
    FB.ui({ method: 'apprequests',
        message: 'Invita a tus amigos a Comunarte...'
    });
}

function graphStreamPublish(mensaje) {
    var body = mensaje;
    FB.api('/me/feed', 'post', { message: body }, function(response) {
        /*if (!response || response.error) {
            alert('Error occured');
        } else {
            alert('Post ID: ' + response.id);
        }*/
    });
}

function fqlQuery(){
    FB.api('/me', function(response) {
         var query = FB.Data.query('select name, hometown_location, sex, pic_square from user where uid={0}', response.id);
         query.wait(function(rows) {
           
           document.getElementById('name').innerHTML =
             'Your name: ' + rows[0].name + "<br />" +
             '<img src="' + rows[0].pic_square + '" alt="" />' + "<br />";
         });
    });
}

function setStatus(){
    status1 = document.getElementById('status').value;
    FB.api(
      {
        method: 'status.set',
        status: status1
      },
      function(response) {
        if (response == 0){
            alert('Your facebook status not updated. Give Status Update Permission.');
        }
        else{
            alert('Your facebook status updated');
        }
      }
    );
}

